/*
 * Copyright (C) 2013 yixia.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.simon.mengkou.ui.activity;


import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;


import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseFullActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

public class VideoPlayerActivity extends BaseFullActivity {
	@Bind(R.id.surface_view)
	VideoView mVideoView;
	//视频地址
	private String mUrl;
	private String mId;

	@Override
	protected void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		mUrl = getIntent().getStringExtra(CstOuer.KEY.URL);
		mId = getIntent().getStringExtra(CstOuer.KEY.ID);
		Vitamio.isInitialized(this);
	}

	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_video_player);
	}

	@Override
	protected void initViews() {
		ButterKnife.bind(this);

		if (UtilString.isBlank(mUrl)) {
			UtilOuer.toast(this, R.string.video_player_error);
			finish();
		} else {
			/*
			 * Alternatively,for streaming media you can use
			 * mVideoView.setVideoURI(Uri.parse(URLstring));
			 */
			mVideoView.setVideoPath(mUrl);
			MediaController mediaController = new MediaController(this);
			mediaController.setOnNavListener(new MediaController.OnNavListener() {
				@Override
				public void onNav() {
					finish();
				}
			});
			mVideoView.setMediaController(mediaController);
			mVideoView.requestFocus();

			mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mediaPlayer) {
					// optional need Vitamio 4.0
					mediaPlayer.setPlaybackSpeed(1.0f);
				}
			});

			videoPlay();
		}
	}


	private void videoPlay() {
		if(UtilString.isBlank(mId)) {
			return;
		}

		AgnettyFuture future = OuerApplication.mOuerFuture.videoPlay(mId, null);
		attachDestroyFutures(future);
	}

}
